package com.example.springstudy.controller;

import com.example.springstudy.common.PagedResultDto;
import com.example.springstudy.common.SuccessResult;
import com.example.springstudy.dto.DataEventInput;
import com.example.springstudy.entity.Disease;
import com.example.springstudy.entity.DiseaseDetail;
import com.example.springstudy.services.DiseaseDetailRepository;
import com.example.springstudy.services.DiseaseRepository;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Optional;

// 数据报警控制器
@RestController
@RequestMapping("api/disease")
public class DiseaseController {
    @Resource
    private DiseaseRepository diseaseRepository;
    @Resource
    private DiseaseDetailRepository diseaseDetailRepository;

    @PostMapping
    public SuccessResult<Disease> addOne(@RequestBody Disease disease) {
//        disease.setId(UUID.fromString(disease.getCommonId()));
        diseaseRepository.save(disease);
        disease.setCommonId(disease.getId()); // commonId(病害id ) 和 id(病害主键) 一致
        diseaseRepository.save(disease);

        DiseaseDetail diseaseDetail = DiseaseDetail.builder()
                // 先给默认值
                .compNo("ZRZH") // 工程构件编码 (先给自然灾害 )
                .shorterRoadLineName("阳县边坡") // 路线名称
                .roadSectionType("边坡") // 路段类型
                .facilityTypeName("边坡") // 设施类型
                .stateName(disease.getStateName()) //  状态名称
                .repairTime(disease.getRepairTime()) // 报修时间
                .flowCompanyName("自然灾害 ")// 报修单位
                .hasAccidentStr("否") // 是否有事故
                .unitName("无") // 单位名称
                .num(BigDecimal.valueOf(1)) // 数量
                .diseasePosition("阳县边坡") // 病害位置
                .emergencyDegree("一般") // 紧急程度
                .reportUnit("阳县边坡") // 报告单位
                .flowUserName("worker") // 报修人
                .beforeRepairImgPath("") // 图片信息

                // 自己添加的
                .insId(disease.getInsId())
                .commonId(disease.getId())
                .diseaseTypeName(disease.getDiseaseTypeName())
                .diseaseRemark(disease.getDiseaseRemark())
                .latitude(disease.getLatitude())
                .longitude(disease.getLongitude())
                .bimElevation(disease.getBimElevation())
                .build();
        diseaseDetailRepository.save(diseaseDetail);

        return new SuccessResult<>(disease);
    }


    @GetMapping("{id}")
    public SuccessResult<Disease> getOne(@PathVariable long id) {
        Optional<Disease> byId = diseaseRepository.findById(id);
        if (byId.isPresent()) {
            return new SuccessResult<>(byId.get());
        }
        throw new RuntimeException(" 病害 not found");
    }

    @GetMapping("")
    public SuccessResult<PagedResultDto<Disease>> getList(DataEventInput input) {
        List<Disease> list = (List<Disease>) diseaseRepository.findAll();
        PagedResultDto<Disease> pageRes = new PagedResultDto<>();
        pageRes.setItems(list);
        pageRes.setTotal((long) list.size());
        return new SuccessResult<>(pageRes);
    }

}
